package com.example.dao.implDao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dao.EnrollInfoDao;
import com.example.mapper.EnrollInfoMapper;
import com.example.pojo.EnrollInfo;
import com.example.pojo.User;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Repository
public class EnrollInfoDaoImpl implements EnrollInfoDao {
    @Resource
    private EnrollInfoMapper enrollInfoMapper;
    @Override
    public List<EnrollInfo> getAllEnroll() {
        return enrollInfoMapper.selectList(null);
    }

    @Override
    public IPage<EnrollInfo> getAllEnrollPage(int currentPage, int limit) {
        // 创建分页对象
        Page<EnrollInfo> page = new Page<>(currentPage, limit);
        return enrollInfoMapper.selectPage(page,null);
    }

    @Override
    public IPage<EnrollInfo> getEnrollByTitle(int currentPage, int limit, String enrollByTitle) {
        Page<EnrollInfo> userPage = new Page<>(currentPage,limit);
        QueryWrapper<EnrollInfo> enrollQueryWrapper = new QueryWrapper<>();
        enrollQueryWrapper.likeRight("enroll_title",enrollByTitle);
        return enrollInfoMapper.selectPage(userPage, enrollQueryWrapper);
    }

    @Override
    public int addEnroll(EnrollInfo enrollInfo) {
     return enrollInfoMapper.insert(enrollInfo);
    }

    @Override
    public int batchDeleteEnrollInfo(List<Integer> ids) {
        return enrollInfoMapper.deleteBatchIds(ids);
    }

    @Override
    public String addOrUpdateEnrollInfo(EnrollInfo enrollInfo) {
        String result = "";
        System.out.println(enrollInfo.getEnrollId());
        if(enrollInfo.getEnrollId()==0) {
            enrollInfoMapper.insert(enrollInfo);
            result="添加成功";
        }else{
            enrollInfoMapper.updateById(enrollInfo);
            result="修改成功";
        }
        return result;
    }

    @Override
    public int deleteEnrollInfo(int eid) {
        return enrollInfoMapper.deleteById(eid);
    }


}
